﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Condimar.Entities;
using Condimar.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Condimar.UI
{
    public partial class FrmRegistrarCategoria : Form
    {
        [Dependency]
        public ICategoriaBL cateBL { get; set; }

        public FrmRegistrarCategoria()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool b;

        public void NuevaCategoria()
        {
            b = true;

            categoriaBindingSource.AddNew();
        }

        public Categoria _categoria { get; set; }

        public void EditarCategoria()
        {
            b = false;

            categoriaBindingSource.DataSource = _categoria;
        }

        private void FrmRegistrarCategoria_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var categoria = categoriaBindingSource.Current as Categoria;

            if ((descripcionTextBox.Text).Length != 0)
            {
                if (categoria != null)
                {
                    if (b == true)
                    {
                        cateBL.AddCategoria(categoria);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    if (b == false)
                    {
                        cateBL.UpdateCategoria(categoria);
                        MessageBox.Show("Modificación Correcta", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                }
            }
            else
                MessageBox.Show("Debe completar todos los campos", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
